package SEApp.boxes.albums
{
	import SEApp.se_util;
	
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_tabItem;
	import UIFree.uif_component;
	
	import flash.geom.Rectangle;

	public class se_albums extends se_abox {
		private var _uid			: String;
		private var _albums			: Array;
		private var _displayCount	: Object;
		private var _isFriends		: Boolean;
		
		public function se_albums(parent: uif_component, uid: String, list:Array, a_displayCount: Object=9, a_isFriends: Boolean=true,
								ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, 
								atabwidth:int=0, styleClass:String='') {
			_uid = uid;
			_displayCount = a_displayCount;
			_isFriends	  = a_isFriends;
			super(parent, seApp.albumsPrepare(uid, list), ax, ay, awidth, aheight, atabwidth, styleClass);
			var inner: Rectangle = getInner();
			if (!_albums || (_albums.length == 0)) {
				(new uif_label(this, inner.x, inner.y, inner.width, inner.height, 'box_label')).text = app.classes.data.NOALBUMS;
				seApp.alert(app.classes.data.WARNING, app.classes.data.NOALBUMMESSAGE);
			}
		}
		
		override protected function maxWidthTab(list: Array): int {
			var itStyle: Object = app.styleByName(style['tabitem_class']);
			var _tlist: Array = new Array();
			for each (var item: Object in list) 
				_tlist.push(item.smallTitle?item.smallTitle:item.title);
				
			return super.maxWidthTab(_tlist);
		}
		
		override protected function createItem(list: Array, inner: Rectangle, index: int, styleName: String): uif_tabItem {
			inner.inflate(9, 0);
			inner.x += 9;
			var item: uif_tabItem = new uif_tabItem(tabs.scrollArea, inner, styleName);
			item.text = list[index].smallTitle;
			return item; 
		}
		
		protected function get friendClass(): Class {
			return se_userAvatar;
		}
		
		protected function get photoClass(): Class {
			return se_userPhoto;
		}
		
		protected function getFriends(): void {
			seApp.externalServer.getAllFriends(app['externalServer'].viewer_id, function(data: *): void {
				assignData(data, friendClass);
			}, 'getAllFriends');
		}
		
		public function assignAlbums(a_uid: String, list: Array): void {
			_uid = a_uid;
			assignTabs(list);
		}
		
		public function get uid(): String {
			return _uid;
		}
		
		override public function assignTabs(list: Array): void {
			if (list.length > 0) {
				_albums = list;
				if (_isFriends && (list != null)) _albums = [{title: app.classes.data.FRIEND, smallTitle: app.classes.data.FRIEND, itemClass: null}].concat(list);
				super.assignTabs(_albums);
				if (_albums && (_albums.length > 0)) tabs.selectIndex = _isFriends?1:0;
			}
		}
		
		protected function assignData(data: Array, itemClass: Class): void {
			
			var curCount: uint;
			if (_displayCount is Array) 
				curCount = _displayCount[(tabs.selectIndex<_displayCount.length)?tabs.selectIndex:_displayCount.length - 1];
			else  curCount = uint(_displayCount);
			var inner: Rectangle = _itemsLayer.displayRect;
			var itemWidth	: int = Math.ceil(inner.width / curCount) + 1;
			var rows		: int = Math.round(inner.height / itemWidth);
			if (rows == 0) rows = 1;
			
			while (data.length < curCount * rows) data.push(null);
			
			_itemsLayer.assignList(data, Math.floor(inner.width / curCount), function(_scroll: uif_component, inner: Rectangle, index: int): se_previewItem {
				return new itemClass(_scroll, inner.x, inner.y, inner.width, inner.height, data[index], 'userPhoto');
			}, rows);
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			if (_isFriends && (tabIndex == 0)) {
				getFriends();
			} else {
				tabs.disable = true;
				seApp.externalServer.getPhotos(_uid, _albums[tabIndex].aid, function(data:*): void {
					assignData(data, photoClass);
					tabs.disable = false;
				});
			}
		}		
	}
}